
import { type Router } from "vue-router";



const whiteList = ['/login', "/404"]
/**
 * 使用 router  每一次切换路由的时候  做判断 
 * @param router 
 */
export default function usePermission(router: Router) {

    router.beforeEach((to, from, next) => {

        const { path } = to;
        const token = sessionStorage.getItem("token");

        //是否登录
        if (token) {

            // console.log('to,from,next', to, from, next)

            next()
        } else {

            //白名单，放行的
            if (whiteList.indexOf(path) != -1) {
                next()
            } else {
                //非登录、切不是白名单，需要去登录
                next(`/login?redirect=${path}`)
            }



        }

    })

}